package com.amqr.movetest.activity;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.amqr.movetest.widget.FoldView;
import com.amqr.movetest.widget.MyRecyclerLayout;
import com.amqr.movetest.R;
import com.amqr.movetest.widget.SwitchViewPager;

import java.util.ArrayList;

public class TestFlowActivity extends AppCompatActivity {

    private FoldView mFv;
    private MyRecyclerLayout mRvOut;
    private SwitchViewPager mVpager;
    private ArrayList<View> pageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_flow);

        mFv = (FoldView) findViewById(R.id.mFv);
        mRvOut = (MyRecyclerLayout) mFv.findViewById(R.id.mRvOut);
        mVpager = (SwitchViewPager) mFv.findViewById(R.id.mVpager);

        mVpager.setIsCanScroll(false);

        mFv.setOpenStateLisner(new FoldView.OpenStateListener() {
            @Override
            public void changeState(boolean state) {
                if(state){
                    mRvOut.setInterceptState(false);
                    mVpager.setIsCanScroll(true);

                }else{
                    mRvOut.setInterceptState(true);
                    mVpager.setIsCanScroll(false);

                }
            }

            @Override
            public void releaseState() {

            }
        });


        initVp();


        mVpager.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               boolean foldState = mFv.getFoldState();
               if(foldState){
                   Toast.makeText(TestFlowActivity.this,"点击顶部",Toast.LENGTH_SHORT).show();
               }
           }
       });


    }

    private void initVp() {
        // 获取不同布局文件的View视图
        View view1 = LayoutInflater.from(TestFlowActivity.this).inflate(
                R.layout.item_vp1, null);
        View view2 = LayoutInflater.from(TestFlowActivity.this).inflate(
                R.layout.item_vp2, null);

        // 将view装入到一个集合中
        pageView = new ArrayList<View>();
        pageView.add(view1);
        pageView.add(view2);


        PagerAdapter pagerAdapter = new PagerAdapter() {
            @Override
            public int getCount() {
                 return pageView.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }



            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                ((ViewPager) container).addView(pageView.get(position));
                return pageView.get(position);
            }


            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                //super.destroyItem(container, position, object);
                ((ViewPager) container).removeView(pageView.get(position));
            }
        };

        mVpager.setAdapter(pagerAdapter);

        mVpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


}
